<?php
/* OS SYSTEM CLASS
 * @autor	Albert Moreno Vendrell
 */
class WebMotor extends Core {
	protected $bbdd_connection	= false;
	protected $applications		= array();
    protected $currentApplication;

    private $configurationFiles    = array(
        "../../core/_includes/configuration.php", 
        "_includes/configuration.php"
    );

    static $_instance;
	
  	public function __construct($application_name=""){
        $this->loadSystem();
  		if($application_name==""){
  			$application_name	= $this->default_app;
  		}
		$this->openApplication($application_name);
  	}
	
	public function __destruct(){
		$this->stopSystem();
	}

    /*
     * We avoid the cloning of the object. Singleton pattern
     */
    private function __clone(){ }

    /*
     * @autor                           Albert Moreno Vendrell
     */
    public function getCurrentApplication(){
        $var=$this->currentApplication;
        return $var;
    }

    /*
     **************************************
     *************** SYSTEM ***************
     **************************************
     */
        /*
         * Start system
         * Function responsible for creating, if necessary, the object. 
         * This is the function must be called from outside the class to instantiate the object, and thus able to use its methods
         * @autor                           Albert Moreno Vendrell
         */
        public function startSystem($automaticApplicationsStart=array()){
            if (!(self::$_instance instanceof self)){
                self::$_instance=new self();
            }

            //AT FIRTS EXECUTION THE PATH IS DIFERENT
            if(in_array("WebMotor",$automaticApplicationsStart)){
                $this->configurationFiles    = array(
                    "core/_includes/configuration.php"
                );
            }

            //AUTOMATIC START APPLICATIONS AFTER START
            foreach($automaticApplicationsStart as $application_name){
                $this->openApplication($application_name);
            }
            return self::$_instance;
        }

        /*
         * Set system parameters
         * @autor                           Albert Moreno Vendrell
         */
        private function loadSystem(){
            $configuration      = Configuration::getInstance($this->configurationFiles);
            $this->default_app 	= $configuration->getDefaultApp();
        }

        /*
         * @autor                           Albert Moreno Vendrell
         */
        private function restartSystem(){
            $this->stopSystem();
            $this->startSystem();
        }

        /*
         * Stop system
         * @autor                           Albert Moreno Vendrell
         */
        private function stopSystem(){
            $this->db->close();
        }

    /*
     **************************************
     ************ APPLICATIONS ************
     **************************************
     */
        /*
         * Open application
         * @autor                           Albert Moreno Vendrell
         */
        public function openApplication($application_name){
            if(!isset($this->applications[$application_name])){
                $this->applications[$application_name]   = new Application($application_name);
            }else{
                $this->applications[$application_name]->openApplication();
            }
        }
}